/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yurendao.OSFileSystem.view;

import com.yurendao.OSFileSystem.bean.FolderBean;
import com.yurendao.OSFileSystem.data.DataAttribute;
import com.yurendao.OSFileSystem.MyFAT.FATStructure;
import com.yurendao.OSFileSystem.MyFAT.MyFAT;
import com.yurendao.OSFileSystem.data.MyFIT;
import com.yurendao.OSFileSystem.file.FileAttributes;
import com.yurendao.OSFileSystem.folder.FolderAttributes;
import com.yurendao.OSFileSystem.file.MyFile;
import com.yurendao.OSFileSystem.folder.MyFolder;
import com.yurendao.OSFileSystem.myList.MyListCellRenderer;
import com.yurendao.OSFileSystem.myTree.ChangeString;
import com.yurendao.OSFileSystem.myTree.MyFileSystemTree;
import com.yurendao.OSFileSystem.service.FATService;
import com.yurendao.OSFileSystem.service.exception.FATServiceException;
import com.yurendao.OSFileSystem.view.NotePad;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import static javafx.scene.text.Font.font;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.api.SubstanceConstants.ImageWatermarkKind;
import org.jvnet.substance.api.SubstanceSkin;
import org.jvnet.substance.skin.CremeSkin;
import org.jvnet.substance.skin.SubstanceRavenGraphiteGlassLookAndFeel;
import org.jvnet.substance.watermark.SubstanceImageWatermark;

/**
 *
 * @author Administrator
 */
public class FileSystem extends javax.swing.JFrame implements FATService {

    private boolean isSelectedFolder = false;
    private int fatNumberOfFolder = 0;
    private MyFAT myfat = new MyFAT();
    MyFileSystemTree mytree;
    private TreePath selectedpath;
    private MyFIT myfit;
    private DefaultListModel listModel = new DefaultListModel();
    private ListCellRenderer renderer = new MyListCellRenderer();
    private int fatNumberOfFile = 0;
    private boolean isSelectedFile = false;
    ArrayList<FATStructure> selectedNodes = new ArrayList<FATStructure>();
    ImageIcon foldericon = new ImageIcon("image\\folder.jpg");
    ImageIcon fileicon = new ImageIcon("image\\file.jpg");

    /**
     * Creates new form os1
     */
    public FileSystem() {
        initComponents();
        setLocation(150, 50);
        this.createdIt();

        jListfile.setCellRenderer(renderer);
        this.jListfile.setPreferredSize(new Dimension(200, 200));
        this.jListfile.setLayoutOrientation(jListfile.HORIZONTAL_WRAP);
        this.jListfile.setVisibleRowCount(3);
        //JList 的 Item 选中监听器
        this.jListfile.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
            }
        });
        //树的 结点选中 监听器
        this.jTree1.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                actionToTreeSelection(e);
            }
        });
        //System.out.println("AA");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    //@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
       //mouse_ContextMenu = new javax.swing.JPopupMenu();
        jPopupMenu1 = new javax.swing.JPopupMenu();
       // jListfile = new javax.swing.JList();
        JMenuItem menuItem1 = new JMenuItem();
        JMenuItem menuItem2 = new JMenuItem();
        JMenuItem menuItem3 = new JMenuItem();
        JMenuItem menuItem4 = new JMenuItem();
        JMenuItem menuItem5 = new JMenuItem();
        JMenuItem menuItem6 = new JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListfile = new javax.swing.JList();
        menuItem1.setLabel("打开");
        menuItem2.setLabel("新建文件");
        menuItem3.setLabel("新建文件夹");
        menuItem4.setLabel("删除");
        menuItem5.setLabel("重命名");
        menuItem6.setLabel("属性");
        jListfile.setModel(new javax.swing.AbstractListModel() {
                String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
                @Override
                public int getSize() { return strings.length; }
                @Override
                public Object getElementAt(int i) { return strings[i]; }
            });
        
        jListfile.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jListFilesMouseClicked(evt);
                }
            });
       
        menuItem1.addActionListener(new java.awt.event.ActionListener() {      //菜单项单击事件
            public void actionPerformed(ActionEvent e) {
                //System.out.println("打开");
                openItActionPerformed(e);
            }
        });
        
        
        menuItem2.addActionListener(new java.awt.event.ActionListener() {      //菜单项单击事件
            public void actionPerformed(ActionEvent e) {
                 createdNewFileActionPerformed(e);
            }
        });
        menuItem3.addActionListener(new java.awt.event.ActionListener() {      //菜单项单击事件
            public void actionPerformed(ActionEvent e) {
               
                try {
                    createFolder();
                } catch (FATServiceException ex) {
                    Logger.getLogger(FileSystem.class.getName()).log(Level.SEVERE, null, ex);
                }
              
            }
        });
        menuItem4.addActionListener(new java.awt.event.ActionListener() {      //菜单项单击事件
            public void actionPerformed(ActionEvent e) {
                System.out.println("删除");
                 deletedItActionPerformed(e);
                
            }
        });
        menuItem5.addActionListener(new java.awt.event.ActionListener() {      //菜单项单击事件
            public void actionPerformed(ActionEvent e) {
                System.out.println("重命名");
                renameItActionPerformed(e);
            }
        });
        menuItem6.addActionListener(new java.awt.event.ActionListener() {      //菜单项单击事件
            public void actionPerformed(ActionEvent e) {
                System.out.println("属性");
             folderAttributeActionPerformed(e);
            }
        });
        jPopupMenu1.add(menuItem1);
        jPopupMenu1.add(menuItem2);
        jPopupMenu1.add(menuItem3);
        jPopupMenu1.add(menuItem4);
        jPopupMenu1.add(menuItem5);
        jPopupMenu1.add(menuItem6);//popmenu中添加菜单项

        jListfile.add(jPopupMenu1);
        jListfile.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(MouseEvent e) {                       //文件图标添加鼠标单击事件
                int mods = e.getModifiers();
                if ((mods & InputEvent.BUTTON3_MASK) != 0) {   //鼠标右键
                    jPopupMenu1.show(jListfile, e.getX(), e.getY());
                }
            }
        });
        jScrollPane3 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jScrollPane1 = new javax.swing.JScrollPane();
        磁盘块 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("文件路径：");

        jTextField1.setText("C:\\");
        jTextField1.addActionListener((java.awt.event.ActionEvent evt) -> {
            jTextField1ActionPerformed(evt);
            System.out.println("文件路径");
        });

        jScrollPane2.setViewportView(jListfile);

        jScrollPane3.setViewportView(jTree1);

        磁盘块.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null}
                },
                new String[]{
                    " 磁盘块", " 索引"
                }
        ) {
            boolean[] canEdit = new boolean[]{
                false, false
            };

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane1.setViewportView(磁盘块);

        jLabel2.setText("     磁盘监视");
        jLabel2.setAlignmentY(0.8F);
        jLabel2.setMinimumSize(new java.awt.Dimension(90, 15));
        jLabel2.setName(""); // NOI18N

        jLabel3.setText("已打开文件");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null}
                },
                new String[]{
                    "文件名称", "打开属性", "起始盘块号", "文件路径"
                }
        ) {
            boolean[] canEdit = new boolean[]{
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane4.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 576, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(6, 6, 6)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextField1)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 348, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 348, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(127, 127, 127)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(142, 142, 142)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 11, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(17, 17, 17))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 394, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jScrollPane3)
                                .addComponent(jScrollPane2))
                        .addContainerGap())
        );

        jMenu1.setText("文件");

        jMenuItem2.setText("退出系统");
        jMenu1.add(jMenuItem2);
        jMenuItem2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == jMenuItem2) {
                    System.out.println("关闭成功！");
                    FileSystem.this.dispose();

                }
            }

        });

        jMenuBar1.add(jMenu1);

        jMenu2.setText("帮助");

        jMenuItem3.setText("关于");
        jMenu2.add(jMenuItem3);
        jMenuItem3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == jMenuItem3) {
                    System.out.println("帮助");
                    new ShowMessage();
                }
            }

        });

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        SubstanceImageWatermark watermark = new SubstanceImageWatermark(FileSystem.class.getResourceAsStream("/images/25.jpg"));
        watermark.setKind(ImageWatermarkKind.SCREEN_CENTER_SCALE);
        watermark.setOpacity((float) 0.9); //更改水印透明度
        try {
            UIManager.setLookAndFeel(new SubstanceRavenGraphiteGlassLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(FileSystem.class.getName()).log(Level.SEVERE, null, ex);
        }
        SubstanceSkin skin = new CremeSkin().withWatermark(watermark);

        //此语句设置外观
        SubstanceLookAndFeel.setSkin(skin);

        pack();
    }// </editor-fold>                        

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }
   private void jListFilesMouseClicked(java.awt.event.MouseEvent evt) {                                        
        // TODO add your handling code here:
        this.actionToJListSelection(evt);
    }        
   private void openItActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        //System.out.println("打开");
        if (this.jListfile.getSelectedIndex() >= 0) {
            openFile(this.jListfile.getSelectedIndex());
        }
    }                                      
   private void createdNewFileActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        //System.out.println("新建一个文件9999999999");
        try {
            this.createFile();
        } catch (FATServiceException ex) {
            Logger.getLogger(FileSystem.class.getName()).log(Level.SEVERE, null, ex);
        }
    }       
    private void deletedItActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
       // System.out.println("删除文件或者文件夹");
        if (this.jListfile.getSelectedIndex() >= 0) {
            try {
                deleteFile(this.jListfile.getSelectedIndex());
            } catch (FATServiceException ex) {
                Logger.getLogger(FileSystem.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }       
    private void renameItActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        if (this.jListfile.getSelectedIndex() >= 0) {
            try {
                rename(this.jListfile.getSelectedIndex());
            } catch (FATServiceException ex) {
                Logger.getLogger(FileSystem.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    private void folderAttributeActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
      //  System.out.println("显示文件夹属性");
        if (this.jListfile.getSelectedIndex() >= 0) {
            this.showFileAttribute(this.jListfile.getSelectedIndex());
        }
    }                       
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(FileSystem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(FileSystem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(FileSystem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(FileSystem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new FileSystem().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jListfile;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTree jTree1;
   // private javax.swing.JPopupMenu mouse_ContextMenu;
    private javax.swing.JTable 磁盘块;
    // End of variables declaration      

    //对 Tree 的选择事件 进行的操作
    //包括：在JList中 显示  文件夹和文件的 图标
    //更改 默认的选中项
    //修改 选中文件的 显示路径
    private void actionToTreeSelection(TreeSelectionEvent e) {
        listModel.removeAllElements();
        //DefaultMutableTreeNode parent = (DefaultMutableTreeNode)this.jTreeFileSystem.getLastSelectedPathComponent();
        TreePath path = e.getNewLeadSelectionPath();
        if (path == null) {
            path = this.selectedpath;
        } else {
            this.selectedpath = path;
        }
        //取得 某个树路径  在MyFAT表中对应的内容
        this.jTree1.expandPath(this.selectedpath);
        ArrayList temp = ChangeString.toFilePath(this.selectedpath.toString());
        int number = myfit.getFATNumber(temp);
        System.out.println("当前选中的结点的为：" + this.selectedpath);
        FATStructure chooseFAT = myfat.getTheFile(number);
        //判断 该内容是 哪种文件
        if (chooseFAT.getFatType() == DataAttribute.FAT_aFolder || chooseFAT.getFatType() == DataAttribute.FAT_aDisk) {
            ArrayList<FATStructure> nodes = myfat.getAllfilesOfOneFolder(number);
            this.selectedNodes.clear();
            int rowcount = ((int) jListfile.getSize().getWidth()) / DataAttribute.List_Icon_width;
            jListfile.setVisibleRowCount(nodes.size() % rowcount > 0 ? (nodes.size() / rowcount + 1) : nodes.size() / rowcount);
            for (int i = 0; i < nodes.size(); i++) {
                FATStructure node = nodes.get(i);
                this.selectedNodes.add(node);
                if (node.getFatType() == DataAttribute.FAT_aFolder) {
                    String foldername = ((MyFolder) (node.getFatContent())).getMyfolderAttributes().getFolderName();
                    listModel.addElement(new Object[]{foldericon, "文件夹", foldername, ChangeString.toTreePath(this.selectedpath, new DefaultMutableTreeNode(foldername)), ((MyFolder) (node.getFatContent())).getMyFATNumber()});
                } else if (node.getFatType() == DataAttribute.FAT_aFile) {
                    String filename = ((MyFile) (node.getFatContent())).getMyfileAttributes().getFileName();
                    listModel.addElement(new Object[]{fileicon, "文件", filename, ChangeString.toTreePath(this.selectedpath, new DefaultMutableTreeNode(filename)), ((MyFile) (node.getFatContent())).getMyFATNumber()});
                }
            }
            this.fatNumberOfFolder = number;
            this.isSelectedFolder = true;
            this.isSelectedFile = false;
            jTextField1.setText(ChangeString.toPath(this.selectedpath.toString()));
        } else if (chooseFAT.getFatType() == DataAttribute.FAT_aFile) {
            this.fatNumberOfFile = number;
            this.isSelectedFolder = false;
            this.isSelectedFile = true;
        }
        //jListFiles.setSelectedIndex(0);
    }

     private void actionToJListSelection(MouseEvent evt) {
        //左键双击与右键单击
        if (evt.getButton() == MouseEvent.BUTTON1) {
            if (evt.getClickCount() == 2) {
                //双击打开
                openFile(this.jListfile.getSelectedIndex());
            } else if (evt.getClickCount() == 1) {
                //单击选中
            }
        } else if (evt.getButton() == MouseEvent.BUTTON3 && evt.getClickCount() == 1) {
            //显示 右键菜单      e.getX() e.getY()   得到的是 鼠标相对于 父控件的位置
            //此处是相对于 JList    最好是 取得与 显示器之间的绝对位置
              jPopupMenu1.show(jListfile, evt.getX(), evt.getY());
        }
    }
    
    private void createdIt() {
        //初始化 MyFAT MyFIT
        //注明：MyFAT为文件分配表  MyFIT为文件索引表
        //this.chushihua();
        this.myfit = new MyFIT(this.myfat);
        //初始化 JList JTree
        // 为JList 添加 ListModel    
        //
        // this.jListFiles.setModel(listModel);
        //利用MyFileSystemTree 根据myfat 初始化JTree       
        //设置 JTree为 单选模式
        this.mytree = new MyFileSystemTree(myfat, jTree1);
        this.jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        //各种 控件的 监听器的初始化
        //this.actionToMouse();
        //this.actionToWindows();
    }

    public void openFile(int number) {
        if (this.isSelectedFolder) {
            Object[] temp = ((Object[]) (this.listModel.getElementAt(number)));
            System.out.println(temp[1]);
            switch (temp[1].toString()) {
                case "文件夹":
                    System.out.println("打开的文件夹是：" + this.selectedpath);
                    this.selectedpath = (TreePath) temp[3];
                    this.jTree1.setSelectionPath(this.selectedpath);
                    System.out.println("打开的文件夹是：" + this.selectedpath);
                    break;
                case "文件":
                    FATStructure fatfile = this.myfat.getTheFile(Integer.parseInt(temp[4].toString()));
                    MyFile file = (MyFile) fatfile.getFatContent();
                    NotePad notepad = new NotePad(temp[2].toString(), file, mytree, myfat, myfit, jTree1);
                    notepad.setVisible(true);
                    notepad.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    break;
            }
        }
    }

    @Override
    public void createFile() throws FATServiceException {
        //To change body of generated methods, choose Tools | Templates.
        System.out.println(111);
        if (this.isSelectedFolder) {
            int j = 0;
            String filename = "新建文件";
            if (!this.selectedNodes.isEmpty()) {
                for (int i = 0; i < this.selectedNodes.size(); i++) {
                    if (this.selectedNodes.get(i).getFatType() == DataAttribute.FAT_aFile) {
                        String name = ((MyFile) (this.selectedNodes.get(i).getFatContent())).getMyfileAttributes().getFileName();
                        if (filename.equals(name)) {
                            j++;
                        }
                        filename = "新建文件" + j;
                    }
                }
            }
            FileAttributes newFileAttributes
                    = this.createdFileAttributes(filename, this.jTextField1.getText(), new Date());
            this.createdFile(this.fatNumberOfFolder, 1, "", newFileAttributes);
            //更新 myfiletree
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) this.jTree1.getLastSelectedPathComponent();
            if (parent == null) {
                System.out.println(this.selectedpath);
                parent = (DefaultMutableTreeNode) this.selectedpath.getLastPathComponent();
            } else {
            }
            this.mytree = new MyFileSystemTree(myfat, jTree1);
//            parent.add(new DefaultMutableTreeNode(filename));
//            ((DefaultTreeModel)(this.jTreeFileSystem.getModel())).reload();
            this.jTree1.setSelectionPath(selectedpath);
            //更新 myfit

            this.myfit = new MyFIT(this.myfat);
        }
        System.out.println("22222");
    }

    private void createdFile(int parentFAT, int numberofFAT, String filedata, FileAttributes newFile) {
        myfat.createdFile(parentFAT, numberofFAT, filedata, newFile);
        
       // System.out.println("11111111");
    }

    private FileAttributes createdFileAttributes(String fileName, String fileLocation, Date fileCreatedTime) {
        FileAttributes result = new FileAttributes(fileName, fileLocation, fileCreatedTime);
       // System.out.println("00000000000");
        return result;
    }

    @Override
    public void deleteFile(int FatNumber) throws FATServiceException {
        // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        if (this.isSelectedFolder) {
            Object[] temp = (Object[]) this.listModel.getElementAt(FatNumber);
            int myFATNumber = Integer.parseInt(temp[4].toString());
            if (temp[1].toString().equals("文件")) {
                this.myfat.deletedFile(myFATNumber);
            } else if (temp[1].toString().equals("文件夹")) {
                if (this.myfat.getAllfilesOfOneFolder(myFATNumber).size() > 0) {
                    JOptionPane.showMessageDialog(null, "请您不能删除空文件夹", "警告", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    this.myfat.deletedFolder(myFATNumber);
                }
            }
            this.mytree = new MyFileSystemTree(myfat, jTree1);
            this.jTree1.setSelectionPath(selectedpath);
            this.myfit = new MyFIT(this.myfat);
        }
    }

    @Override
    public void updateFile(int myFATNumber, String name) {
        this.myfat.updateFile(myFATNumber, name);
        this.mytree = new MyFileSystemTree(myfat, jTree1);
        ((DefaultTreeModel) (this.jTree1.getModel())).reload();
        this.jTree1.setSelectionPath(selectedpath);
        this.myfit = new MyFIT(this.myfat);
    }

    @Override
    public void createFolder() throws FATServiceException {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        if (this.isSelectedFolder) {
            //更新 myfat
            int j = 0;
            String foldername = "新建文件夹";
            if (!this.selectedNodes.isEmpty()) {
                for (int i = 0; i < this.selectedNodes.size(); i++) {
                    if (this.selectedNodes.get(i).getFatType() == DataAttribute.FAT_aFolder) {
                        String name = ((MyFolder) (this.selectedNodes.get(i).getFatContent())).getMyfolderAttributes().getFolderName();
                        if (foldername.equals(name)) {
                            j++;
                        }
                        foldername = "新建文件夹" + j;
                    }
                }
            }
            FolderAttributes newFolderAttributes
                    = this.createdFolderAttributes(foldername, this.jTextField1.getText(), new Date());
            this.createdFolder(this.fatNumberOfFolder, newFolderAttributes);
            //更新 myfiletree
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) this.jTree1.getLastSelectedPathComponent();
            System.out.println("当前选中的树节点为：" + parent);
            if (parent == null) {
                System.out.println(this.selectedpath);
                parent = (DefaultMutableTreeNode) this.selectedpath.getLastPathComponent();
            } else {
            }
            this.mytree = new MyFileSystemTree(myfat, jTree1);
//            parent.add(new DefaultMutableTreeNode(foldername));
//            ((DefaultTreeModel)(this.jTreeFileSystem.getModel())).reload();
            this.jTree1.setSelectionPath(selectedpath);
            System.out.println("------打开的文件夹是：" + this.selectedpath);
            //新建文件夹时 更新JList
            //更新 myfit
            this.myfit = new MyFIT(this.myfat);
        }
    }

    //用于 右键菜单--新建文件夹  调用
    private void createdFolder(int parentFAT, FolderAttributes newFolder) {
        myfat.createdFolder(parentFAT, newFolder);
        //myfat.showMyFAT();
    }

    //创建 某文件夹的 属性
    private FolderAttributes createdFolderAttributes(String folderName, String folderLocation, Date folderCreatedTime) {
        FolderAttributes result = new FolderAttributes(folderName, folderLocation, folderCreatedTime);
        return result;
    }

    @Override
    public void delteFolder(int FatNumber) throws FATServiceException {
       // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

        if (this.isSelectedFolder) {
            Object[] temp = (Object[]) this.listModel.getElementAt(FatNumber);
            int myFATNumber = Integer.parseInt(temp[4].toString());
            if (temp[1].toString().equals("文件")) {
                this.myfat.deletedFile(myFATNumber);
            } else if (temp[1].toString().equals("文件夹")) {
                if (this.myfat.getAllfilesOfOneFolder(myFATNumber).size() > 0) {
                    JOptionPane.showMessageDialog(null, "该文件夹不为空，不能删除！！！！", "警告！！", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    this.myfat.deletedFolder(myFATNumber);
                }
            }
            this.mytree = new MyFileSystemTree(myfat, jTree1);
            this.jTree1.setSelectionPath(selectedpath);
            this.myfit = new MyFIT(this.myfat);
        }

    }

    @Override
    public void updateFolder(int myFATNumber, String name) throws FATServiceException {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        myfat.updateFolder(myFATNumber, name);
        this.mytree = new MyFileSystemTree(myfat, jTree1);
        ((DefaultTreeModel) (this.jTree1.getModel())).reload();
        this.jTree1.setSelectionPath(selectedpath);
        this.myfit = new MyFIT(myfat);
    }

    @Override
    public void rename(int FatNumber) throws FATServiceException {
        // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.         if (this.isSelectedFolder) {
        Object[] temp = (Object[]) this.listModel.getElementAt(FatNumber);
        //JTextFiled filename =
        String filename = temp[2].toString();
        String newname = JOptionPane.showInputDialog(null, "请输入新的文件名或文件夹： ", "重命名", JOptionPane.INFORMATION_MESSAGE);
        if (newname != null) {
            if (filename.equals(newname)) {

            } else {
                if (!this.selectedNodes.isEmpty() && temp[1].toString().equals("文件或文件夹")) {
                    int i;
                    for (i = 0; i < this.selectedNodes.size(); i++) {
                        if (this.selectedNodes.get(i).getFatType() == DataAttribute.FAT_aFile) {
                            String name = ((MyFile) (this.selectedNodes.get(i).getFatContent())).getMyfileAttributes().getFileName();
                            if (newname.equals(name)) {
                                JOptionPane.showMessageDialog(null, "亲,文件夹或文件名不能重复", "警告！！", JOptionPane.INFORMATION_MESSAGE);
                                break;
                            }
                        }
                    }
                    if (i >= this.selectedNodes.size()) {
                        temp[2] = newname;
                        updateFile(Integer.parseInt(temp[4].toString()), newname);
                    }
                } else if (!this.selectedNodes.isEmpty() && temp[1].toString().equals("文件夹")) {
                    int i;
                    for (i = 0; i < this.selectedNodes.size(); i++) {
                        if (this.selectedNodes.get(i).getFatType() == DataAttribute.FAT_aFolder) {
                            String name = ((MyFolder) (this.selectedNodes.get(i).getFatContent())).getMyfolderAttributes().getFolderName();
                            if (newname.equals(name)) {
                                JOptionPane.showMessageDialog(null, "当前位置包含同名文件夹！！！", "警告！！", JOptionPane.INFORMATION_MESSAGE);
                                break;
                            }
                        }
                    }
                    if (i >= this.selectedNodes.size()) {
                        temp[2] = newname;
                        updateFolder(Integer.parseInt(temp[4].toString()), newname);
                    }
                }
            }
        }
    }

    private void showFileAttribute(int number) {
        if (this.isSelectedFolder) {
            Object[] temp = ((Object[]) (this.listModel.getElementAt(number)));
            System.out.println(temp[1]);
            if (temp[1].toString().equals("文件夹")) {
                FATStructure fatfolder = this.myfat.getTheFile(Integer.parseInt(temp[4].toString()));
                MyFolder folder = (MyFolder) fatfolder.getFatContent();
                FolderAttractiveView folderview = new FolderAttractiveView(new JFrame(), true, folder);
                folderview.setVisible(true);
                folderview.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            } else if (temp[1].toString().equals("文件")) {
                FATStructure fatfile = this.myfat.getTheFile(Integer.parseInt(temp[4].toString()));
                MyFile file = (MyFile) fatfile.getFatContent();
                FileAttractiveView fileview = new FileAttractiveView(new JFrame(), true, file);
                fileview.setVisible(true);
                fileview.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            }
        }
    }

//       private void guardIt() {
//        ShowFAT showfat = new ShowFAT(this.myfat);
//        showfat.setVisible(true);
//        showfat.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//    }
    @Override
    public FATStructure getTheFile(int myFATNumber) throws FATServiceException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

    }

    @Override
    public ArrayList<FATStructure> getAllfilesOfOneFolder(int myFatherFATNumber) throws FATServiceException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

    }
    
}
